# coding: utf-8

# 生成地推用户

import os

import set_env_path     # 请勿删除

from sqlalchemy import create_engine
from sqlalchemy.sql import text

from common.wechat_util import client


engine = create_engine(
    os.getenv('DATABASE_URL')
    or 'mysql+pymysql://root:@127.0.0.1/wph_www_prod?charset=utf8'
)
conn = engine.connect()


def insert_ground_users():
    insert_internal_user_sql = text("""
        insert into user (
            mobile, nickname, wx_openid, area_id,
            type, gender, created_at, updated_at
        ) values (
            :mobile, :nickname, :wx_openid, :area_id,
            :type, :gender, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
        );
    """)

    for row in range(1, 241):
        idx = '{:0>3}'.format(row)
        mobile = 'WPSD_DT_{}'.format(idx)
        nickname = '地推人员{}'.format(idx)

        conn.execute(
            insert_internal_user_sql,
            mobile=mobile,
            nickname=nickname,
            wx_openid=mobile,
            area_id=440300,
            type=201,
            gender=1,
        )
        resp = client.qrcode.create({
            'action_name': 'QR_LIMIT_STR_SCENE',
            'action_info': {
                'scene': {'scene_str': mobile}
            }
        })
        url = resp['url']
        print('{}: {}'.format(idx, url))


if __name__ == '__main__':
    insert_ground_users()
